//
//  AnnounceViewController.m
//  YiBaoStore
//
//  Created by 吕清毅 on 2018/10/4.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "AnnounceViewController.h"
#import "EditModel.h"

@interface AnnounceViewController ()<UITableViewDelegate,UITableViewDataSource,AnnounceTableViewCellDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIView *btnBackView;
@property (nonatomic, strong) AddAnnounceViewController *addVC;
@end

@implementation AnnounceViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNaviWith:@"店铺公告"];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refresh:) name:NoticeRefresh object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
   // [super dealloc];
}

#pragma mark -- 通知刷新
- (void)refresh:(NSNotificationCenter *)noti
{
    [self getData];
}


- (void)getData
{
    self.dataArray = [NSMutableArray array];
    [[HttpRequest currentHttpRequest]postPath:Notice_List_API params:nil resultBlock:^(id Data, MsgModel *msgModel) {
        NSArray *array = [NSArray arrayWithArray:Data];
        for (NSInteger i = 0; i < array.count; i++) {
            AnnounceModel *model = [AnnounceModel mj_objectWithKeyValues:array[i]];
            [self.dataArray addObject:model];
        }
        if (self.dataArray.count == 0) {
            self.tableView.tableFooterView = [self setNoDataView];
        }else
        {
            self.tableView.tableFooterView = [UIView new];
        }
         [self.tableView reloadData];
        
    }];
    [self setUpTable];
    [self setUpAddBtn];
}

- (UIView *)setNoDataView
{
    UIView *view = [UIView new];
    view.frame = CGRectMake(0, 0, SCREENWIDTH, 100);
    view.backgroundColor = TotalWhite;
    
    UILabel *label = [UILabel new];
    label.frame = CGRectMake(0, 40, SCREENWIDTH,20);
    label.textColor = titleColor;
    label.font = FontWithSize(16);
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"暂无公告";
    [view addSubview:label];
    return view;
}

- (void)setUpAddBtn
{
    if (self.btnBackView==nil) {
        UIView *view = [UIView new];
        view.frame = CGRectMake(SCREENWIDTH-80, SCREENHEIGHT-65-54, 65, 65);
        view.backgroundColor = UIColorFromHex(0xF7D553);
        view.layer.cornerRadius = 65/2;
        view.layer.masksToBounds = YES;
        self.btnBackView = view;
        [self.view addSubview:view];
        
        UIImageView *addIV = [UIImageView new];
        addIV.frame = CGRectMake((65-16)/2, 17, 16, 16);
        addIV.image = [UIImage imageNamed:@"addAnnounce"];
        [view addSubview:addIV];
        
        UILabel *addLab = [UILabel new];
        addLab.frame = CGRectMake((65-40)/2, addIV.bottom+5, 40, 10);
        addLab.textColor = UIColorFromHex(0x3E3E3E);
        addLab.font = FontWithSize(10);
        addLab.text = @"添加公告";
        [view addSubview:addLab];
        
        UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        addBtn.frame = CGRectMake(0, 0, view.width, view.height);
        addBtn.backgroundColor = ClearColor;
        [addBtn addTarget:self action:@selector(addBtn:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:addBtn];
    }
}

- (void)setUpTable
{
    if (self.tableView == nil) {
        if (@available(iOS 11.0, *)) {
            self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,TopBarHeight_xs, SCREENWIDTH, SCREENHEIGHT-TopBarHeight_xs-BottomHeight_xs) style:UITableViewStylePlain];
        } else {
            // Fallback on earlier versions
            self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,TopBarHeight, SCREENWIDTH, SCREENHEIGHT-TopBarHeight-BottomHeight) style:UITableViewStylePlain];
        }
        self.tableView.backgroundColor = TotalWhite;
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.estimatedRowHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
        [self.tableView registerClass:[AnnounceTableViewCell class] forCellReuseIdentifier:@"AnnounceTableViewCell"];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.rowHeight = 90;
        if (self.dataArray.count == 0) {
             self.tableView.tableFooterView = [self setNoDataView];
        }else
        {
            self.tableView.tableFooterView = [UIView new];
        }
        [TTKUtil setAutomaticallyAdjustsScrollViewInsetsWidth:self.tableView withView:self];
        //self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getNewData)];
        [self.view addSubview:self.tableView];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AnnounceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AnnounceTableViewCell"];
    cell.delegate = self;
    cell.indexPath = indexPath;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    AnnounceModel *model = self.dataArray[indexPath.row];
    [cell getData:model];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)deleteThisAnnounce:(NSInteger)tagId and:(NSIndexPath *)indexPath
{
    NSLog(@"tagid = %li index = %li",tagId,indexPath.row);
    NSMutableDictionary *data = [NSMutableDictionary new];
    [data setValue:[NSNumber numberWithInteger:tagId] forKey:@"id"];
    [HttpRequest formPostPath:Notice_Remove_API params:data resultBlock:^(id Data, MsgModel *msgModel) {
        [self ShowHUDWithMessage:@"删除成功"];
        [self.dataArray removeObjectAtIndex:indexPath.row];
        [self getData];
    }];
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 90;
//}

- (void)addBtn:(UIButton *)btn
{
     self.addVC = [[AddAnnounceViewController alloc]init];;
    [self.view addSubview:self.addVC.view];
     self.addVC.view.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT);
}

@end
